//
//  LBTabBar.m
//  LvoeBeen_Imitation
//
//  Created by Mac on 17/2/18.
//  Copyright © 2017年 itcast. All rights reserved.
//

#define TabBarItemCount 4
#import "LBTabBar.h"

@implementation LBTabBar

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self creatShoppingButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat tabBarButtonWidth = ScreenWidth / TabBarItemCount;
    NSInteger index = 0;
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            subView.frame = CGRectMake(tabBarButtonWidth * index, 0, tabBarButtonWidth, TabBarHeight);
            index += index == 1 ? 2 : 1;
        }
    }
}

- (void) clickShoppingButton {
    if ([self.showShoppingTrollyDelegate respondsToSelector:@selector(showShoppingTrolly:)]) {
        [self.showShoppingTrollyDelegate showShoppingTrolly:self];
    }
}


- (void) creatShoppingButton {
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth * 2 / TabBarItemCount, 0, ScreenWidth / TabBarItemCount, TabBarHeight)];
    
    [button setImage:[UIImage imageNamed:@"shopCart"] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"shopCart_r"] forState:(UIControlStateHighlighted)];
    CGFloat edgeInsetsMargin = (button.bounds.size.width - 25) / 2;
    [button setImageEdgeInsets:(UIEdgeInsetsMake(6, edgeInsetsMargin, 18, edgeInsetsMargin))];
    
    [button setTitle:@"购物车" forState:(UIControlStateNormal)];
    [button setTitleEdgeInsets:(UIEdgeInsetsMake(36, 0, 1, 31/2))];
    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor yellowColor] forState:(UIControlStateHighlighted)];
    button.titleLabel.font = [UIFont systemFontOfSize:10];
    
    [button addTarget:self action:@selector(clickShoppingButton) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self addSubview:button];
}

@end
